<template>
  <view class='login'>
	  
	  <view class="lo_icon">
	  	<view class="lo_image">
	  		<image src="../../static/common/icon.png" mode=""></image>
	  	</view>
		<view class="lo_text">
			<image src="../../static/common/iconfont.png" mode=""></image>
		</view>
	  </view>
	  
	  <view class="regbutton_login"  @click='wechatLogin' v-if="H5Wechat">
	  	<button class="button"  open-type='getUserInfo'>微信快捷登录/注册</button>
	  </view>
	  <view class="regbutton_login" @click="clickonlog">
	  	<button   class="regbutton">账号快捷登录/注册</button>
	  </view>
	  
	  
	  <view class="lo_font">
	  	毕业设计
	  </view>
	  
  </view>
</template>

<script>
	import md5 from '../../common/md5.js'
export default {
  data() {
    return {
		providers:'',
		H5Wechat: true
	};
  },
  onLoad() {
   // #ifdef H5
			this.H5Wechat = false
   // #endif
  },
  methods: {
	  // 跳转到账号密码页面
	  clickonlog(){
		  //在起始页面跳转到test.vue页面并传递参数
		  uni.navigateTo({
		    url:"../theLogin/index"
		  });
	  },
	  //微信快捷登陆
	  async wechatLogin(){
		 let appKey = 123456789
		 let app_secret= 'abcdef'
		 let nonce = Math.random().toString(36).substr(2)
		 let  time_stamp = Date.parse(new Date())/1000
		 let getData = {}
		  uni.login({
		     provider: 'weixin',
		    success:  (loginRes) => {
		      // 获取用户信息
		      uni.getUserInfo({
		        provider: 'weixin',
		        success:  (infoRes) => {
				 // #ifdef MP-WEIXIN
				 	getData={
					 code:loginRes.code,
					 encryptedData:infoRes.encryptedData,
					 iv:infoRes.iv,
					 time_stamp:time_stamp,
					 nonce:nonce,
					 signature:md5(`app_key=`+appKey+`&app_secret=`+app_secret+`&nonce=`+nonce+`&time_stamp=`+time_stamp),
					 app_key:appKey 
				 }
				 // #endif
				
				// #ifdef APP-PLUS
					getData={
						 time_stamp:time_stamp,
						 nonce:nonce,
						 signature:md5(`app_key=`+appKey+`&app_secret=`+app_secret+`&nonce=`+nonce+`&time_stamp=`+time_stamp),
						 app_key:appKey,
					     user_info:JSON.stringify(infoRes.userInfo)
					}
				// #endif 
				
				
				  this.request({
					  url:'/v1/token/wechat',
					  method:'POST',
					  data:getData
				  }).then((res)=>{
					  if(res.code === 200){
						  uni.setStorage({
							 key: 'myinfo',
							 data: res.data,
							 success() {
							 }
						  })
						  uni.switchTab({
						  	 url:'../index/index'
						  })
					  }else{
						  uni.showToast({
							  title:res.desc,
							  icon:"none"
						  })
					  }
				  })
				 
		        },
				
		      });
		    }
		  });
		  
	  }
  },
};
</script>

<style lang="less" >
	.login{
	}
	.lo_icon{
		margin-bottom: 95rpx;
		margin-top: 120rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;
		.lo_image{
			margin-bottom: 22rpx;
			image{
				width: 124rpx;
				height: 124rpx;
			}
		}
		.lo_text{
			image{
				width: 196rpx;
				height: 54rpx;
			}
		}
	}
	.regbutton_login{
		width: 630rpx;
		height: 48rpx;
		text-align: center;
		line-height: 48rpx;
		border: #b2b2b2 1rpx;
		margin: 70rpx auto;
	}
	.lo_font{
		width: 100%;
		position: absolute;
		bottom: 145rpx;
		font-size: 24rpx;
		color: #808080;
		text-align: center;
	}
	.button{
		background-color: #65bb25;
		font-size: 32rpx;
		color: #FFFFFF;
	}
	.regbutton{
		background-color: #FFFFFF;
		font-size: 32rpx;
		color: #4D4D4D;
	}
</style>
